﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PeoplesJukebox.Core.Interfaces;
using PeoplesJukebox.Core.Configuration;
using PeoplesJukebox.Core.TrackLists;

namespace PeoplesJukebox.Core
{
    /// <summary>
    /// Simple IOC container
    /// </summary>
    public static class Container
    {
        private static IMediaCatalogue _catalogue;
        private static IMediaPlayer _player;
        private static List<ITrackList> _trackLists;
        private static SelectedTrackList _selectedTrackList;
        private static RandomTrackList _randomTrackList;

        /// <summary>
        /// Returns the configured <see cref="IMediaCatalogue"/> implementation
        /// </summary>
        public static IMediaCatalogue Catalogue
        {
            get { return _catalogue; }
        }

        /// <summary>
        /// Returns the configured <see cref="IMediaPlayer"/> implementation
        /// </summary>
        public static IMediaPlayer Player
        {
            get { return _player; }
        }

        /// <summary>
        /// Returns the configured collection of <see cref="ITrackList"/> implementations
        /// </summary>
        public static List<ITrackList> TrackLists
        {
            get { return _trackLists; }
        }

        /// <summary>
        /// Returns the selected tracklist
        /// </summary>
        public static SelectedTrackList SelectedTrackList
        {
            get { return _selectedTrackList; }
        }

        /// <summary>
        /// Returns the random tracklist
        /// </summary>
        public static RandomTrackList RandomTrackList
        {
            get { return _randomTrackList; }
        }

        /// <summary>
        /// Static constructor, populates dependency objects
        /// </summary>
        static Container()
        {
            _catalogue = GetInstance<IMediaCatalogue>(PeoplesJukeboxConfiguration.Current.MediaCatalogue);
            _trackLists = new List<ITrackList>();
            foreach (TrackList trackList in PeoplesJukeboxConfiguration.Current.TrackLists)
            {
                ITrackList list = GetInstance<ITrackList>(trackList.Type);
                list.Load();
                list.ChosenByText = trackList.ChosenByText;
                _trackLists.Add(list);
                if (list is SelectedTrackList)
                {
                    _selectedTrackList = (SelectedTrackList) list;
                }
                else if (list is RandomTrackList)
                {
                    _randomTrackList = (RandomTrackList)list;
                }
            }
            _player = GetInstance<IMediaPlayer>(PeoplesJukeboxConfiguration.Current.MediaPlayer);
        }

        private static T GetInstance<T>(string typeName)
        {
            T instance = default(T);
            try
            {
                Type type = Type.GetType(typeName);
                instance = (T)Activator.CreateInstance(type);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format("Error loading type: {0}, {1}", typeName, ex.Message), ex);
            }
            return instance;
        }
    }
}